package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

/**
 * Created by Administrator on 2016/1/23.
 */
public class SelectCityAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private Context context;
    String[] provices;
    String[][] citys;

    public SelectCityAdapter(Context context, String[] provices, String[][] citys) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.provices = provices;
        this.citys = citys;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_city_item, parent, false);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.mTextView.setText(citys[groupPosition][childPosition]);
        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return citys[groupPosition][childPosition];
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return citys[groupPosition].length;
    }


    @Override
    public Object getGroup(int groupPosition) {
        return provices[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return provices.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_provice_item, parent, false);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.mTextView.setText(provices[groupPosition]);
        if (isExpanded) {
            holder.mImageView.setBackgroundResource(R.drawable.ic_arrow_up);
        } else {
            holder.mImageView.setBackgroundResource(R.drawable.ic_arrow_down);
        }
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class MyViewHolder {
        MyTextView mTextView;
        MyImageView mImageView;

        MyViewHolder(View view) {
            mTextView = (MyTextView) view.findViewById(R.id.mtv_content);
            mImageView = (MyImageView) view.findViewById(R.id.miv_image);
        }
    }
}
